<?php

namespace app\mp\controller;

use app\common\entity\BackResult;
use app\common\entity\Result;
use app\Request;
use think\facade\Db;

/**
 * Class User
 * @package app\mp\controller
 */
class User extends Base
{
    /**
     * 获取我的空间页面中的笔记、翻译、视频、单词列表数据
     * @param Request $request
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(Request $request)
    {
        $type = $request->param('type');
        $page = (int)$request->param('page');

        switch ($type) {
            case 'note':
                $rows = [];
                // 先使用统计的方式查询，并且分页
                $videoIds = Db::table('video_note')
                    ->where('uid', $this->user['id'])
                    ->field('video_id')
                    ->group('video_id')
                    ->order('id', 'desc')
                    ->page($page, 10)
                    ->select()
                    ->toArray();
                if ($videoIds) {
                    $videoIds = array_column((array)$videoIds, 'video_id');
                    $videos = Db::table('video_list')
                        ->alias('v')
                        ->leftJoin('user_study us', 'us.video_id = v.id')
                        ->whereIn('v.id', $videoIds)
                        ->field('v.id,v.title,v.cover_url,v.duration,us.progress')
                        ->select();
                    foreach ($videos as $video) {
                        // 由于每条视频的笔记都要全查，查询量太大，所以这里用缓存
                        $cacheKey = "VIDEO_NOTE:{$this->user['id']}:{$video['id']}";
                        if (!($notes = cache($cacheKey))) {
                            // 没有缓存数据则执行查询
                            $notes = Db::table('video_note')
                                ->where('video_id', $video['id'])
                                ->where('uid', $this->user['id'])
                                ->field('id,note')
                                ->order('id', 'desc')
                                ->select()
                                ->toArray();
                            if ($notes) {
                                // 加入缓存
                                cache($cacheKey, $notes);
                            }
                        }
                        $video['note'] = $notes;
                        $rows[] = $video;
                    }
                }
                break;
            case 'translate':
                $rows = [];
                break;
            case 'video':
                $rows = Db::table('user_favor_video')
                    ->alias('uf')
                    ->leftJoin('video_list vl', 'uf.video_id = vl.id')
                    ->where('uf.uid', $this->user['id'])
                    ->order('uf.update_time', 'desc')
                    ->field('vl.id,vl.title,vl.duration,vl.hot,vl.cover_url')
                    ->page($page, 10)
                    ->select();
                break;
            case 'word':
                $rows = DB::table('user_favor_word')
                    ->alias('ufw')
                    ->leftJoin('word_dict wd', 'wd.id = ufw.word_id')
                    ->where('ufw.uid', $this->user['id'])
                    ->order('id', 'desc')
                    ->page($page, 10)
                    ->field('ufw.id,wd.word,wd.cn')
                    ->select();
                break;
            default:
                return BackResult::error(null, '参数异常');
        }

        return Result::success($rows);
    }
}